package com.itheima.im.socket.activity;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;

import com.itheima.im.socket.R;
import com.itheima.im.socket.adapter.ChatAdapter;
import com.itheima.im.socket.bean.QQMessage;
import com.itheima.im.socket.bean.QQMessageType;
import com.itheima.im.socket.core.OnReceiveMsgListener;
import com.itheima.im.socket.utils.ThreadUtils;

public class ChatActivity extends Activity {
	private long toChatAccount = 0;
	private String toChatNick = "";
	@InjectView(R.id.title)
	TextView title;
	@InjectView(R.id.chatlistview)
	ListView chatlistview;
	@InjectView(R.id.input)
	EditText input;

	@OnClick(R.id.send)
	public void send(View view) {

		String inputMessage = input.getText().toString().trim();
		input.setText("");

		// 创建消息
		final QQMessage msg = new QQMessage();
		msg.type = QQMessageType.MSG_TYPE_CHAT_P2P;
		msg.content = inputMessage;
		msg.from = Long.parseLong(MyApp.username);
		msg.to = toChatAccount;
		messages.add(msg);
		setAdapterOrNotify();
		ThreadUtils.runInThread(new Runnable() {

			@Override
			public void run() {
				try {
					MyApp.conn.sendMessage(msg);
				} catch (Exception e) {
					e.printStackTrace();
				}

			}
		});

	}
	private OnReceiveMsgListener listener = new OnReceiveMsgListener() {
		public void onReceive(final QQMessage msg) {//子线程
			
			ThreadUtils.runUIThread(new Runnable() {
				@Override
				public void run() {
					if (msg.type.equals(QQMessageType.MSG_TYPE_CHAT_P2P)) {
						messages.add(msg);
						setAdapterOrNotify();
						Toast.makeText(getBaseContext(), "好友消息:"+msg.content, 0).show();
					}
					
				}
			});

		}
	};

	protected void onDestroy() {
		super.onDestroy();
		MyApp.conn.removeOnReceiveMsgListener(listener);
	};

	private List<QQMessage> messages = new ArrayList<QQMessage>();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_chat);
		ButterKnife.inject(this);

		MyApp.conn.addOnReceiveMsgListener(listener);

		Intent intent = getIntent();
		toChatAccount = intent.getLongExtra("tochatAccount", 0);
		toChatNick = intent.getStringExtra("tochatNick");
		title.setText("与" + toChatNick + "聊天中...");
		// intent.putExtra("tochatNick", buddy.nick);
		// intent.putExtra("tochatAccount", buddy.account);//取出长整型

		// // x 5 发送 老王 from MyApp.username
		//
		// for (int i = 0; i < 5; i++) {
		// QQMessage msg = new QQMessage();
		// msg.type = QQMessageType.MSG_TYPE_CHAT_P2P;
		// msg.content = "发送:i" + i;
		// msg.from = Long.parseLong(MyApp.username);
		// msg.to = toChatAccount;
		// messages.add(msg);
		// }
		// // x 5 接收 大头 from 不等 MyApp.username
		// for (int i = 0; i < 5; i++) {
		// QQMessage msg = new QQMessage();
		// msg.type = QQMessageType.MSG_TYPE_CHAT_P2P;
		// msg.content = "接收数据" + i;
		// msg.to = Long.parseLong(MyApp.username);
		// msg.from = toChatAccount;
		// messages.add(msg);
		// }

		setAdapterOrNotify();
	}

	ChatAdapter adapter = null;

	private void setAdapterOrNotify() {
		if (adapter == null) {
			if (messages.size() < 1) {
				return;
			}
			adapter = new ChatAdapter(this, messages);
			chatlistview.setAdapter(adapter);

		} else {
			adapter.notifyDataSetChanged();// 刷新
		}

		// 指向最后一条聊天记录
		if (messages.size() >= 1) {
			chatlistview.setSelection(messages.size() - 1);
		}

	}
}
